package jp.co.jalinfotec.sxt.form;

import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/** Entry user request form */
public class EntryUserActionForm extends ActionForm{

	/** Serial Version UID */
	private static final long serialVersionUID = 1L;
	
	/** User ID */
	private String userId;
	/** User name */
	private String userName;
	/** Password*/
	private String password;
	
	/**
	 * [Getter]User ID
	 * @return User ID
	 */
	public String getUserId() {return userId;}
	
	/**
     * [Setter]User ID
     * @param User ID
     */
	public void setUserId(String userId) {this.userId = userId;}
	
	/**
	 * [Getter]User name
	 * @return User name
	 */
	public String getUserName() {return userName;}
	
	/**
     * [Setter]User name
     * @param User name
     */
	public void setUserName(String userName) {this.userName = userName;}

	/**
	 * [Getter]Password
	 * @return Password
	 */
	public String getPassword() {return password;}
	
	/**
	 * [Setter]Password
	 * @param password Password
	 */
	public void setPassword(String password) {this.password = password;}
	
	/** Default constructor. */
	public EntryUserActionForm(){
		super();
	}
	
	/**
	 * Initialize Properties
	 * @param mapping Mapping info between URL and Action
	 * @param request Request
	 */
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.userId = null;
		this.userName = null;
		this.password = null;
	}
	
	/**
	 * Validate input values. 
	 * TODO: Use MessageResources.
	 * @param mapping Mapping info between URL and Action
	 * @param request Request
	 */
	@Override
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		
		ActionErrors errors = new ActionErrors();
				
		if (this.userId == null || this.userId.trim().length() == 0){
			errors.add(ActionMessages.GLOBAL_MESSAGE,	new ActionMessage("user.error.required", "User ID"));
		}
		else if (!Pattern.compile("\\d{5,6}").matcher(this.userId).find()){
			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("user.error.user_id"));
		}
		if (this.userName == null || this.userName.trim().length() == 0){
			errors.add(ActionMessages.GLOBAL_MESSAGE,	new ActionMessage("user.error.required", "User name"));
		}
		return errors;
	}
}
